<?php

    // +----------------------------------------------------------------------
    // | 推荐位控制器
    // +----------------------------------------------------------------------
    // | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
    // +----------------------------------------------------------------------
    // | Author: 微尘 <yicmf@qq.com>
    // +----------------------------------------------------------------------

    namespace app\controller\cms\admin;

    use app\model\cms\Position as PositionModel;
    use app\model\cms\PositionData;
    use yicmf\builder\Edit;
    use yicmf\builder\Table;
    use think\facade\Db;
    use think\facade\Url;
    use app\controller\admin\Common;

    class Position extends Common
    {

        protected function initialize()
        {
            parent::initialize();
        }

        public function _filter()
        {
        	return [];
        }

        public function index()
        {
            // 显示页面
            $builder = new Table();
            $builder->model(PositionModel::class)
				->where($this->_filter())
				->warning('推荐位请不要随意更改')
                ->searchText('title', '推荐位名称', '推荐位名称')
                ->buttonUpdate(url('cms/admin/Position/update'))
                ->keyId('id', '编号', 'desc')
                ->keyText('name', '推荐位标记')
                ->keyText('title', '推荐位名称')
                ->keyText('category_id', '所属栏目')
                ->keyText('max_num', '储数数量')
                ->keyText('update_time', '更新时间')
                ->keyStatus()
                ->actionUpdate('info?id={$id}', '信息管理')
                ->actionUpdate('update?id={$id}')
                ->actionRestore('restore?id={$id}')
                ->actionDelete('del?id={$id}')
                ->actionForbid('forbid?id={$id}')
                ->actionToCheck('restore?id={$id}');

            return $builder->fetch();
        }

        public function info()
        {
            // 读取规则列表
            if ( !$this->request->has('id', 'get') ) {
                $this->_returnError( '参数错误');
            } else {
                $data = $this->model->get($this->request->get('id'));
                $list = $data->positiondata()
                    ->where('status', 1)
                    ->select();
                // 显示页面
                $builder = new Table();
                $builder->warning('在此新增仅可以增加链接推荐，文档推荐需要在文档编辑状态下增加')
                    ->buttonUpdate(url('admin.Position/InfoUpdate', ['position_id' => $this->request->get('id')]))
                    ->keyId('id', '编号', 'desc')
                    ->keyTitle()
                    ->keyBool('target', '新窗口')
                    ->keyText('update_time', '更新时间')
                    ->keyStatus()
                    ->data($list, false)
                    ->actionUpdate('InfoUpdate?id={$id}', '信息编辑')
                    ->actionDelete('delData?id={$id}', '移除');

                return $builder->fetch();
            }
        }

        public function delData()
        {
            if ( $this->model->positionDataRemove($this->request->get('id')) ) {
                $this->_returnSuccess( '移除成功');
            } else {
                $this->_returnError( '移除异常：' . $this->model->getError());
            }
        }

        public function InfoUpdate()
        {
            if ( $this->request->isPost() ) {
                $data_model = new PositionData(); // TODO:可以优化为关联更新
                if ( $data_model->updateData($this->request->post()) !== false ) {
                    $this->_returnSuccess( '更新推荐位成功');
                } else {
                    $this->_returnError( '更新推荐位失败，失败信息【' . $data_model->getError() . '】');
                }
            } else {
                // 判断是否为编辑模式
                $isEdit = $this->request->has('id', 'get') ? true : false;
                if ( $isEdit ) {
                    $data = $this->model->positiondata()
                        ->where('id', $this->request->get('id'))
                        ->find();
                } else {
                    $data = [];
                }
                $builder = new Edit();
                $builder->title($isEdit ? '信息编辑' : '信息添加')
                    ->keyTitle()
                    ->keyTextArea('description', '详情描述')
                    ->keyImageModel('pic_url', '对应图片')
                    ->keyNumber('sort', '排序', '数字越大越靠前')
                    ->keyBool('target', '是否新窗口打开')
                    ->keyBool('only_url', '是否独立链接', '非独立链接绑无法修改URL地址')
                    ->keyText('url', 'URL链接')
                    ->data($data)->buttonSubmit('', '保存')
                    ->buttonClose();

                return $builder->fetch();
            }
        }

        public function update()
        {
            if ( $this->request->isPost() ) {
                $info = $this->model->updateData($this->request->post());
                if ( $info !== false ) {
                    $this->_returnSuccess( '更新推荐位成功');
                } else {
                    $this->_returnError( '更新推荐位失败，失败信息【' . $this->model->getError() . '】');
                }
            } else {
                // 判断是否为编辑模式
                $isEdit = $this->request->has('id', 'get') ? true : false;
                if ( $isEdit ) {
                    $data = $this->model->find($this->request->get('id'));
                } else {
                    $data = [];
                }
                $cates = Db::name('Category')->where('style', 2)->column('id,title');
                $builder = new Edit();
                $builder->title($isEdit ? '编辑规则' : '添加规则')
                    ->keyText('name', '推荐位标记', '后台展示用，必须唯一')
                    ->keyText('title', '推荐位名称', '前台展示')
                    ->keySelectMultiple('category_id', '所属栏目', $cates, '不勾选则为所有')
                    ->keyNumber('max_num', '最大存储数据量', '范围0-15，超出自动删除旧数据', [
                        'size' => 5,
                        'max' => 15,
                        'min' => 0,
                        'step' => 1
                    ])
                    ->data($data)
                    ->buttonSubmit('', '保存')
                    ->buttonClose();

                return $builder->fetch();
            }
        }

        public function ajaxGet()
        {
            $lists = $this->model->where('category_id', 'like', '%' . $this->request->post('category_id') . '%')
                ->whereOr('category_id', '')
                ->select();
            if ( $this->request->post('id', 0) ) {
                $has_ids = PositionData::where('document_id', $this->request->post('id', 0))->column('position_id');
                if ( $has_ids ) {
                    $this->assign('has_ids', $has_ids);
                }
            }
            if ( $lists ) {
                $this->assign('lists', $lists);
            }
            return $this->fetch();
        }
    }
